<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use think\Db;
use \think\facade\Session;

/*产品*/
class Product extends Base
{  
    /*列表*/
	  public function index(Request $request){
		   $this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
      
        $this->assign($data);
	      return $this->fetch();
	  }

    public function get_data(Request $request){
        $this->check_session();
        $get=$request->get();

        $start=($get['page']-1)*$get['limit']+1;
        if(empty($get['sear'])){
          $list=Db::table('product_middle_table')->order('id', 'desc')->limit($start,$get['limit'])->select()->toArray();
          $model_data['count']=Db::table('product_middle_table')->count();
        }else{
          $list=Db::table('product_middle_table')->where([['Product_Number','like','%'.$get['sear'].'%']])->order('id', 'desc')->limit($start,$get['limit'])->select()->toArray();
          $model_data['count']=Db::table('product_middle_table')->where([['Product_Number','like','%'.$get['sear'].'%']])->count();
        }
        $model_data['list']=$list;
        

        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
    }

      /*新增 或者 修改 页面*/
    public function change(Request $request){
        $this->check_session();
        /*左侧导航*/
        $data_get=$request->get();
        $data=$this->navleft($data_get['nav']);
        /*品牌和材质*/
        $model=new \app\admin\model\Product();/*调用模型查询*/
        $model_data=$model->get_brand_mater();
        $data['sleinfo']=$model_data;
        /*分类*/
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $class_model_data=$class_model->get_sel(0);
        $data['sleinfo']['classify']=$class_model_data;

        if(isset($data_get['id'])){
          $model_data=$model->get_edit($data_get['id']);
          $data['data_list']=$model_data;
        }
      /*  echo "<pre>";
        var_dump(json($data['data_list']['classify']));
        echo "</pre>";
        exit;*/
        $this->assign($data);
        return $this->fetch();
    }

	    /*执行 新增 或者 修改任务*/
	  public function do_change(Request $request){
		    $this->check_session();
        $data=$request->post();

        if(empty($data['Part_Number'])){
          $result=['status'=>'0','msg'=>'请填写产品名称'];
          return json($result);
        }
        if(empty($data['quiz1'])||$data['quiz1']=='0'){
          $result=['status'=>'0','msg'=>'请选择分类'];
          return json($result);
        }
        
        $model=new \app\admin\model\Product();/*调用模型查询*/
        $get=$request->get();
        if(!empty($get['id'])){
          $model_data=$model->update_data($get['id'],$data);
        }else{
          $model_data=$model->insert_data($data);
        }
        if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
        }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
        }    
	  }
    
     /*删除*/
    public function del(Request $request){
        $this->check_session();
        $data=$request->post();
        if(empty($data['id'])){
            $result=['status'=>'0','msg'=>'操作有误'];
            return json($result);
        }
        $model=new \app\admin\model\Product();/*调用模型查询*/
        $model_data=$model->delete_data($data['id']);
        if($model_data!=1){
            $result=['status'=>'0','msg'=>'删除失败'];
            return json($result);
        }else{
            $result=['status'=>'1','msg'=>'删除成功'];
            return json($result);
        }
    }

    /*seo设置*/
    public function getseo(Request $request){
      $data_get=$request->get()['nav'];
      $data=$this->navleft($data_get);

      $this->check_session();
      $p=$request->get()['p'];
      $id=$request->get()['id'];

      $data['one']=Db::table('product_middle_table')->where('id','=',$id)->find();
      
      
      $model=new \app\admin\model\SpecialModelSeo();/*调用模型查询*/
      $data['detail']=$model->get_data($p);

      $this->assign($data);
      return $this->fetch();

    }

    public function shibie_seo(Request $request){
      $this->check_session();
      $post=$request->post();
      $product=Db::table('product_middle_table')->where('id','=',$post['id'])->find();

      $model=new \app\admin\model\SpecialModelSeo();/*调用模型查询*/
      $data=$model->get_data($product['URL_Product']);
      if(empty($data)){
         $post['URL_Product']=$product['URL_Product'];
         $post['Product_Number']=$product['Product_Number'];
         return $this->insertseo($post);
      }else{
         $post['URL_Product']=$product['URL_Product'];
         return $this->seoupdate($post);
      } 
    }

    /*seo设置*/
    public function seoupdate($post){

      $this->check_session();
      
      $model=new \app\admin\model\SpecialModelSeo();/*调用模型查询*/
      $a=$model->update_data($post);
      if($a!=1){
          $result=['status'=>'0','msg'=>'修改失败'];
          return json($result);
      }else{
          $result=['status'=>'1','msg'=>'修改成功'];
          return json($result);
      }

    }
    public function insertseo($post){

      $this->check_session();
      
      $model=new \app\admin\model\SpecialModelSeo();/*调用模型查询*/
      $a=$model->insert_data($post);
      if($a!=1){
          $result=['status'=>'0','msg'=>'添加失败'];
          return json($result);
      }else{
          $result=['status'=>'1','msg'=>'添加成功'];
          return json($result);
      }

    }

    /*seo设置*/
    public function setSeo(Request $request){
      $this->check_session();
      $data=$request->post();
      if(empty($data['id'])){
          $result=['status'=>'0','msg'=>'无修改id'];
          return json($result);
      }
      if(empty($data['tit'])){
          $result=['status'=>'0','msg'=>'请填写标题'];
          return json($result);
      }
      if(empty($data['key'])){
          $result=['status'=>'0','msg'=>'请填写关键词'];
          return json($result);
      }
      if(empty($data['des'])){
          $result=['status'=>'0','msg'=>'请填写描述'];
          return json($result);
      }
      $model=new \app\admin\model\Product();/*调用模型查询*/
      $model_data=$model->setSeo($data['id'],$data['tit'],$data['key'],$data['des']);
      if($model_data!=1){
          $result=['status'=>'0','msg'=>'修改失败'];
          return json($result);
      }else{
          $result=['status'=>'1','msg'=>'修改成功'];
          return json($result);
      }

    }
    /*附加属性页面*/
    public function productext(Request $request){
      $this->check_session();
      /*左侧导航*/
      $data_get=$request->get();
      $data=$this->navleft($data_get['nav']);
      $Product_ext=new \app\admin\model\Product();/*调用模型查询*/
      if(isset($data_get['id'])){
        $model_data=$Product_ext->get_product_ext($data_get['id']);
        $data['data_list']=$model_data;
        $data['pid']=$data_get['id'];
      }
      $this->assign($data);
      return $this->fetch();
    }

    /*删除属性*/
    public function productext_del(Request $request){
      $this->check_session();
      $data=$request->post();
      if(empty($data['id'])){
        $result=['status'=>'0','msg'=>'操作有误'];
        return json($result);
      }
      $result=Db::name('product_ext')->where([['id','=',$data['id']],['status','=',1]])->data(['status'=>'2'])->update();
      if($result==0){
            $result=['status'=>'0','msg'=>'删除失败'];
            return json($result);
      }else{
            $result=['status'=>'1','msg'=>'删除成功'];
            return json($result);
      }
    }

    /*对属性进行增加或者删除*/
    public function productext_insert_update(Request $request){
      $this->check_session();
      $data=$request->post();
      $pid=$request->get()['pid'];

      foreach ($data as $key => $value) {
        $k=explode("_",trim($key));
        if($k[0]=='old'){$old[$k[1]]['meta_key']=$value;}
        if($k[0]=='oldv'){$old[$k[1]]['meta_value']=$value;}
        if($k[0]=='new'){$new[$k[1]]['meta_key']=$value;}
        if($k[0]=='newv'){$new[$k[1]]['meta_value']=$value;}
      }
      /*老数据修改*/
      if(isset($old) && !empty($old)){
        foreach ($old as $key => $value) {
          if(empty($value['meta_key'])){
            $result=['status'=>'0','msg'=>'请输入附加名称','suo'=>'old_'.$key];
            return json($result);
          }
          if(empty($value['meta_value'])){
            $result=['status'=>'0','msg'=>'请输入附加值','suo'=>'oldv_'.$key];
            return json($result);
          }
          $update=Db::name('product_ext')->where([['id','=',$key],['status','=',1]])->data(['meta_key'=>$value['meta_key'],'meta_value'=>$value['meta_value']])->update();
        }
      }
      /*新数据添加*/
      if(isset($new) && !empty($new)){
        foreach ($new as $key => $value) {
            if(empty($value['meta_key'])){
              $result=['status'=>'0','msg'=>'请输入附加名称','suo'=>'new_'.$key];
              return json($result);
            }
            if(empty($value['meta_value'])){
              $result=['status'=>'0','msg'=>'请输入附加值','suo'=>'newv_'.$key];
              return json($result);
            }
            $data = ['p_id' => $pid, 'meta_key' => $value['meta_key'], 'meta_value'=>$value['meta_value']];
            $insert=Db::name('product_ext')->data($data)->insert();
        }  
      }
      $result=['status'=>'1','msg'=>'操作成功'];
      return json($result);
      
    }

    public function des(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
       
        $data['one']=Db::table('product_middle_table')->where('id','=',$get['id'])->find();
          $model_data['count']=Db::table('product_middle_table')->count();
        $data['post']=$post;
        $this->assign($data);
        return $this->fetch();
      }else{
        
        if($post['y']=='zh'){
          $model_data=Db::table('product_middle_table')->where('id',$post['id'])->update(['zh_txt' => $post['txt'],]);
        }else{
          $model_data=Db::table('product_middle_table')->where('id',$post['id'])->update(['txt' => $post['txt'],]);
        }
        if($model_data==1){
          $result=['status'=>'1','msg'=>'操作成功'];
          return json($result);
        }else{
          $result=['status'=>'0','msg'=>'操作失败系统错误'];
          return json($result);
        }
      }  
    }


}	